package web;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

import com.opensymphony.xwork2.ActionSupport;

public class JfreeChartAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JFreeChart chart;
	
	private static String name;
	private static int age;
	
	public String getJFreeChart(){
		
		System.out.println(name+age);
//		DefaultPieDataset data = new DefaultPieDataset();
//		data.setValue("Java", new Double(43.2));
//		data.setValue("Visual Basic", new Double(1.0));
//		data.setValue("C/C++", new Double(17.5));
//		data.setValue("tangjun", new Double(60.0));
//		// 生成JFreeChart对象
//		chart = ChartFactory.createPieChart("Pie Chart", data, true, true,
//				false);
		
		  DefaultCategoryDataset dataset = new DefaultCategoryDataset();  
	        // 显示一行的所有列，一字型排开  
	        dataset.addValue(1.0, "Row 1", "Column 1");  
	        dataset.addValue(5.0, "Row 1", "Column 2");  
	        dataset.addValue(3.0, "Row 1", "Column 3");  
	        dataset.addValue(2.0, "Row 2", "Column 1");  
	        dataset.addValue(3.0, "Row 2", "Column 2");  
	        dataset.addValue(2.0, "Row 2", "Column 3");  
	          
	        chart = ChartFactory.createBarChart3D("BarChartDemo", // chart title  
	                "横坐标值", // domain axis label  
	                "竖坐标值", // range axis label  
	                dataset, // data  
	                PlotOrientation.VERTICAL, // 图显示的方向为水平显示  
	                true, // include legend  
	                true, // tooltips?  
	                false // URLs?  
	        );  
	        chart.setBackgroundPaint(Color.yellow);  
	          
	        CategoryPlot plot = (CategoryPlot) chart.getPlot();  
	        plot.setBackgroundPaint(Color.GREEN);  
	        plot.setRangeGridlinePaint(Color.red);  // 修改网格线的颜色  
	        BarRenderer renderer = (BarRenderer) plot.getRenderer();  
	        renderer.setItemMargin(0.0);  
	      
	    // 产生标签指示器，1代表列名，2代表数据值，3代表百分比  
	        CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator("{2}", new DecimalFormat("0.0"));  
	        renderer.setBaseItemLabelGenerator(generator);  
	        //renderer.setBaseItemLabelsVisible(true);  // 设置标签是否可见  
	          
	    // 单独指定显示系列的标签  
	        renderer.setBaseItemLabelsVisible(null);    // 清除所有系列的标志，必须得要  
	        renderer.setSeriesItemLabelsVisible(0, true);  
	        renderer.setSeriesItemLabelsVisible(1, false);
	        
	        FileOutputStream fos_jpg = null;
	        try {
				fos_jpg = new FileOutputStream("D://fruit.jpg");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
	           try {
				ChartUtilities.writeChartAsJPEG(fos_jpg, 1.0f, chart, 400, 300,null);
			} catch (IOException e) {
				e.printStackTrace();
			}
		
		return SUCCESS;
	}
	
	public void setData(){
		System.out.println(name+age);
		System.out.println("呵呵");
	}

	public JFreeChart getChart() {
		return chart;
	}

	public void setChart(JFreeChart chart) {
		this.chart = chart;
	}

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	public static void setName(String name) {
		JfreeChartAction.name = name;
	}

	public static void setAge(int age) {
		JfreeChartAction.age = age;
	}
}
